package com.cenwangkeji.zhihuimuye.ui.activity;


import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.constraint.ConstraintLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
import com.cenwangkeji.zhihuimuye.R;
import com.cenwangkeji.zhihuimuye.model.bean.CowBean;
import com.cenwangkeji.zhihuimuye.ui.adapter.MainAdapter;
import com.cenwangkeji.zhihuimuye.ui.adapter.MainLeadAdapter;
import com.cenwangkeji.zhihuimuye.ui.fragment.HomeFragment;
import com.cenwangkeji.zhihuimuye.ui.fragment.MonitoringFragment;
import com.cenwangkeji.zhihuimuye.ui.fragment.StatisticsFragment;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chaychan.library.BottomBarLayout;
import com.lws.basic.base.BaseActivity;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.listener.OnBannerListener;
import com.youth.banner.loader.ImageLoader;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends BaseActivity implements BaseQuickAdapter.OnItemChildClickListener {


    @BindView(R.id.banner)
    Banner mBanner;
    @BindView(R.id.rv)
    RecyclerView mRv;
    @BindView(R.id.vp)
    ViewPager mVp;
    @BindView(R.id.bb)
    BottomBarLayout mBb;
    @BindView(R.id.cl_herdsman)
    ConstraintLayout mHerdsman;


    @Override
    public int setLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    public void initData() {
        super.initData();
        ButterKnife.bind(this);
        int state = getIntent().getIntExtra("data",0 );
        if (state==1){
            showLead();
        }else {
            showHerdsman();
        }
        // showLead();
       // showHerdsman();

    }

    //显示牧民端
    private void showHerdsman(){
        mVp.setVisibility(View.GONE);
        mBb.setVisibility(View.GONE);
        mHerdsman.setVisibility(View.VISIBLE);
        initBanner();
        initRecycler();
    }

    //显示领导端
    private void showLead(){
        mVp.setVisibility(View.VISIBLE);
        mBb.setVisibility(View.VISIBLE);
        mHerdsman.setVisibility(View.GONE);
        mVp.setOffscreenPageLimit(4);
        mVp.setAdapter(new MainLeadAdapter(getSupportFragmentManager(),generateFragments()));
        mBb.setViewPager(mVp);
    }

    private List<Fragment> generateFragments() {
        List<Fragment> fragments = new ArrayList<>();
        fragments.add(new HomeFragment());
        fragments.add(new StatisticsFragment());
        fragments.add(new MonitoringFragment());
        return fragments;
    }


    private void initBanner(){
        List<Integer> list = new ArrayList<>();
        list.add(R.mipmap.bg_banner_1);
        list.add(R.mipmap.bg_banner_2);
        List<String> title = new ArrayList<>();
        title.add("阿荣旗扶贫牧场");
        title.add("阿荣旗扶贫牧场");

        mBanner.setOnBannerListener(new OnBannerListener() {
            @Override
            public void OnBannerClick(int position) {

                Uri uri = Uri.parse("http://news.cctv.com/2019/10/10/ARTIKi6JwN06c0dAYHh8QX50191010.shtml");
                Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                startActivity(intent);


            }
        });
        mBanner.setImageLoader(new ImageLoader() {
            @Override
            public void displayImage(Context context, Object path, ImageView imageView) {
                Glide.with(context).load(path).into(imageView);
            }
        }).setImages(list).setDelayTime(3000).setBannerTitles(title).setBannerStyle(BannerConfig.CIRCLE_INDICATOR_TITLE).setIndicatorGravity(BannerConfig.CENTER).start();

    }

    private void initRecycler(){
        List<CowBean> list = new ArrayList<>();
        CowBean cowBean = new CowBean();
        cowBean.setId("B00001");
        CowBean cowBean1 = new CowBean();
        cowBean1.setId("B00002");
        CowBean cowBean2 = new CowBean();
        cowBean2.setId("B00003");
        list.add(cowBean);
        list.add(cowBean1);
        list.add(cowBean2);
        mRv.setLayoutManager(new LinearLayoutManager(this));
        MainAdapter adapter = new MainAdapter(list);
        adapter.openLoadAnimation(BaseQuickAdapter.SCALEIN);
        adapter.setOnItemChildClickListener(this);
        mRv.setAdapter(adapter);
    }

    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
           switch (view.getId()){
               case R.id.tv_detail:
               case R.id.tv_id:
                   startActivity(new Intent(this,CowDetailActivity.class));
                   break;
               case R.id.iv:
                   startActivity(new Intent(this,VideoActivity.class));
                   break;
           }
    }
}
